package cn.bdqn.controller;

import cn.bdqn.domain.Admin;
import cn.bdqn.service.AdminService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 王浩
 * @create 2020/9/19 10:00
 */
@RestController
public class AdminController {

    @Autowired
    private AdminService service;

    /**
     * 添加管理员信息
     * @param admin
     */
    @PostMapping("/admin/addInfo")
    public Result addInfo(@RequestBody Admin admin) {
        try{
            service.addInfo(admin);
            //返回结果
            return  ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("添加失败");
        }
    }

    /**
     * 根据id查询管理员信息
     * @param id
     * @return
     */
    @GetMapping("/admin/queryInfoById/{id}")
    public Result queryInfoById(@PathVariable("id") Integer id) {
        try{
            //返回结果
            return  ResultGenerator.genSuccessResult(service.queryInfoById(id));
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }
    }

    /**
     * 根据手机号和密码查询管理员信息
     * @return
     */
    @GetMapping("/admin/login/{password}/{phone}")
    public Result queryInfoByPhoneAndPassword(@PathVariable("password") String password,@PathVariable("phone") String phone) {
        try{
            //返回结果
            return  ResultGenerator.genSuccessResult(service.queryInfoByPhoneAndPassword(password,phone));
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }
    }

    /**
     * 根据手机号查询   用于判断手机号是否注册
     * @param phone
     * @return
     */
    @GetMapping("/admin/queryByPhoneIsExist/{phone}")
    public Result queryByPhoneIsExist(@PathVariable("phone") String phone) {
        try{
            //返回结果
            return  ResultGenerator.genSuccessResult(service.queryByPhoneIsExist(phone));
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }
    }

    /**
     * 查询全部并分页
     * @param pageNum
     * @param pageAmount
     * @return
     */
    @GetMapping("/admin/queryAllInfo/{pageNum}/{pageAmount}")
    public Result queryAllInfo(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageAmount") Integer pageAmount) {
        try{
            Page page = service.queryAllInfo(pageNum,pageAmount);
            //返回结果
            return  ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }
    }

    /**
     * 根据管理员名和手机号模糊查询并分页
     * @param name
     * @param phone
     * @param pageNum
     * @param pageAmount
     * @return
     */
    @GetMapping("/admin/queryInfoByNameOrPhone/{name}/{phone}/{pageNum}/{pageAmount}")
    public Result queryInfoByNameOrPhone(@PathVariable("name") String name,
                                         @PathVariable("phone") String phone,
                                         @PathVariable("pageNum") Integer pageNum,
                                         @PathVariable("pageAmount") Integer pageAmount) {

        try{
            Page page = service.queryInfoByNameOrPhone(name.trim(),phone.trim(),pageNum,pageAmount);
            //返回结果
            return  ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }

    }

    /**
     * 修改管理员信息
     * @param admin
     */
    @PutMapping("/admin/alterInfo")
    public Result alterInfo(@RequestBody Admin admin) {
        try{
            service.alterInfo(admin);
            return  ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("修改失败");
        }
    }

    /**
     * 逻辑删除管理员信息
     * @param id
     */
    @PutMapping("/admin/alterInfo/{id}")
    public Result alterStateById(@PathVariable("id") Integer id) {
        try{
            service.alterStateById(id);
            return  ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("更新失败");
        }
    }

}
